package data_structure.video;

class BinaryTreeDemo {
    public static void main(String[] args) {
        //创建一颗二叉树
        BinaryTree binaryTree = new BinaryTree();
        //创建需要的结点
        Node node1 = new Node(1, "松江");
        Node node2 = new Node(2, "吴用");
        Node node3 = new Node(3, "林冲");
        Node node4 = new Node(4, "卢俊义");

        //手动创建二叉树，后面以递归方式创建二叉树（未学目前）
        node1.setLeft(node2);
        node1.setRight(node3);
        node3.setRight(node4);
        binaryTree.setRoot(node1);

        System.out.println("前序遍历");
        binaryTree.preOrder();

        System.out.println("中序遍历");
        binaryTree.infixOrder();

        System.out.println("后序遍历");
        binaryTree.postOrder();
    }

    //定义 BinaryTree 二叉树
    static class BinaryTree{
        private Node root;

        public void setRoot(Node root) {
            this.root = root;
        }

        //前序遍历
        public void preOrder(){
            if (this.root != null)
                this.root.preOrder();
            else
                System.out.println("二叉树为空,无法遍历");
        }

        //中序遍历
        public void infixOrder(){
            if (this.root != null)
                this.root.infixOrder();
            else
                System.out.println("二叉树为空，无法遍历");
        }

        //后续遍历
        public void postOrder(){
            if (this.root != null)
                this.root.postOrder();
            else
                System.out.println("二叉树为空，无法遍历");
        }
  }

    //定义结点
    static class Node{
        private int no;
        private String name;
        private Node left;//默认为null
        private Node right;//默认为null

        public Node(int no, String name) {
            this.no = no;
            this.name = name;
        }

        public int getNo() {
            return no;
        }

        public void setNo(int no) {
            this.no = no;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Node getLeft() {
            return left;
        }

        public void setLeft(Node left) {
            this.left = left;
        }

        public Node getRight() {
            return right;
        }

        public void setRight(Node right) {
            this.right = right;
        }

        @Override
        public String toString() {
            return "Node{" +
                    "no=" + no +
                    ", name='" + name + '\'' +
                    '}';
        }

        //前序遍历方法
        //1.先输出根结点
        //2.如果左子树不为空，则递归继续谦虚遍历
        //3.如果右子树不为空，则递归继续前序遍历
        public void preOrder(){
            System.out.println(this);//先输出父节点
            //递归向左子树前序遍历
            if (this.left != null)
                this.left.preOrder();
            //递归向右子树前序遍历
            if (this.right != null)
                this.right.preOrder();
        }

        //中序遍历
        public void infixOrder(){
            //递归向左子树中序遍历
            if (this.left != null)
                this.left.infixOrder();
            //输出父节点
            System.out.println(this);
            if (this.right != null){
                this.right.infixOrder();
            }
        }

        //后序遍历
        public void postOrder(){
            if (this.left != null)
                this.left.postOrder();
            if (this.right != null)
                this.right.postOrder();
            System.out.println(this);
        }
    }
}
